//
//  AppDelegate.m
//  JPKY
//
//  Created by Vitas on 2016/11/13.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "AppDelegate.h"
#import "JPTabBarController.h"
#import "JPLeftBarController.h"
#import "ICSDrawerController.h"
#import "JPNavigationController.h"
#import "JPLoginLeftController.h"
#import <SMS_SDK/SMSSDK.h>
#import <SVProgressHUD.h>

// SMSSDK官网公共key
#define APPKEY @"f3fc6baa9ac4"
#define APP_SECRET @"7f3dedcb36d92deebcb373af921d635a"

@interface AppDelegate ()

@property (assign,nonatomic) BOOL isLogin;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:@"loginSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutSuccess) name:@"logoutSuccess" object:nil];
    //判断是否已经自动登录
    self.isLogin = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"YES"] ? YES : NO;
//    self.isLogin = NO;

    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window = window;
    [self enter];
    [window makeKeyAndVisible];
    
    // 设置SMS框架
    [SMSSDK registerApp:APPKEY withSecret:APP_SECRET];
    
    return YES;
}

#pragma mark - 通知触发方法

/**
 *  登录成功
 */
- (void)loginSuccess{
    // 设置弹窗消失时间
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    self.isLogin = YES;
    // 保存到用户偏好设置
    [[NSUserDefaults standardUserDefaults] setObject:self.isLogin ? @"YES" : @"NO" forKey:@"isLogin"];
    [self enter];
    // 弹窗提示
    [SVProgressHUD showSuccessWithStatus:@"登陆成功!"];
}

/**
 *  注销成功
 */
- (void)logoutSuccess{
    // 设置弹窗消失时间
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    self.isLogin = NO;
    // 保存到用户偏好设置
    [[NSUserDefaults standardUserDefaults] setObject:self.isLogin ? @"YES" : @"NO" forKey:@"isLogin"];
    [self enter];
    // 弹窗提示
    [SVProgressHUD showSuccessWithStatus:@"注销成功!"];
}

/**
 *  根据登录状态加载左侧滑栏的显示状态
 */
- (void)enter{
    self.isLogin == YES ? [self hasLogin] : [self notLogin];
}

/**
 *  未登录状态
 */
- (void)notLogin {
    JPLoginLeftController *loginController = [[JPLoginLeftController alloc] init];
    JPTabBarController *mainController = [[JPTabBarController alloc] init];
    
    self.window.rootViewController = [[ICSDrawerController alloc]initWithLeftViewController:loginController centerViewController:mainController];
    
    [self.window makeKeyAndVisible];
}

/**
 *  已登录状态
 */
- (void)hasLogin {
    JPLeftBarController *leftController = [[JPLeftBarController alloc] init];
    JPTabBarController *mainController = [[JPTabBarController alloc] init];
    
    self.window.rootViewController = [[ICSDrawerController alloc]initWithLeftViewController:leftController centerViewController:mainController];
    
    [self.window makeKeyAndVisible];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
